#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    int countSubstrings(string s) {
        vector<vector<bool>>dp(s.size(), vector<bool>(s.size(), false));
        int ret = 0;
        for (int i = s.size() - 1; i >= 0; i--)
        {
            for (int j = i; j < s.size(); j++)
            {
                if (s[i] == s[j])
                {
                    if (j - i <= 1 || (j - i > 1 && dp[i + 1][j - 1] == true))
                    {
                        dp[i][j] = true;
                        ret++;
                    }
                }
            }
        }
        return ret;
    }
};

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        vector<vector<int>>dp(s.size(), vector<int>(s.size(), 0));
        for (int i = s.size() - 1; i >= 0; i--)
        {
            for (int j = i; j < s.size(); j++)
            {
                if (s[i] == s[j])
                {
                    if (i == j)
                        dp[i][j] = 1;
                    else if (j - i == 1)
                        dp[i][j] = 2;
                    else
                        dp[i][j] = dp[i + 1][j - 1] + 2;
                }
                else
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
            }
        }
        return dp[0][s.size() - 1];
    }
};